package com.example.tys.baseapplication.View.recycleView.sticky;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;

import com.example.tys.baseapplication.MyApp;

/**
 * LinearLayoutManager,显示title
 *
 * @author tys
 * @date 2017/10/14
 */

public class StickyItemDivider extends RecyclerView.ItemDecoration {

    private int titleTextColor = Color.GREEN;
    private int titleHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 35,
            MyApp.getInstance().getResources().getDisplayMetrics());
    private int titleColor = Color.GRAY;
    private int lineHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1,
            MyApp.getInstance().getResources().getDisplayMetrics());
    private int lineColor = Color.RED;
    private StickyAdapter adapter;
    private Paint paint;

    public StickyItemDivider(@NonNull StickyAdapter adapter) {
        this.adapter = adapter;
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(titleHeight * 0.5f);
    }

    /**
     * 计算 item间间隙(是普通分割线 ,还是title)
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (adapter.isEmpty()) {
            return;
        }
        if (!adapter.needTitle(((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition())) {
            outRect.set(0, lineHeight, 0, 0);
        } else {
            outRect.set(0, titleHeight, 0, 0);
        }
    }

    /**
     * 底层绘制,绘制分栏title
     */
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (adapter.isEmpty()) {
            return;
        }
        super.onDraw(c, parent, state);
        int left = parent.getPaddingLeft();
        int right = parent.getMeasuredWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            int position = ((RecyclerView.LayoutParams) child.getLayoutParams()).getViewLayoutPosition();
            int bottom = child.getTop() - ((RecyclerView.LayoutParams) child.getLayoutParams()).topMargin;
            if (!adapter.needTitle(position)) {
                // 画分割线
                int top = bottom - lineHeight;
                paint.setColor(lineColor);
                c.drawRect(left, top, right, bottom, paint);
            } else {
                //画TITLE
                int top = bottom - titleHeight;
                paint.setColor(titleColor);
                c.drawRect(left, top, right, bottom, paint);
                drawText(c, adapter.getItemViewTitle(position), left + titleHeight * 0.25f, bottom - titleHeight * 0.25f);
            }
        }
    }

    /**
     * 上层绘制,绘制顶部悬停title
     */
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (adapter.isEmpty()) {
            return;
        }
        super.onDrawOver(c, parent, state);
        int left = parent.getPaddingLeft();
        int right = parent.getMeasuredWidth() - parent.getPaddingRight();
        int top = parent.getPaddingTop();
        int bottom = top + titleHeight;
        paint.setColor(titleColor);
        c.drawRect(left, top, right, bottom, paint);
        int pos = ((LinearLayoutManager) (parent.getLayoutManager())).findFirstVisibleItemPosition();
        drawText(c, adapter.getItemViewTitle(pos), left + titleHeight * 0.25f, bottom - titleHeight * 0.25f);
    }

    private void drawText(Canvas c, String itemViewTitle, float x, float y) {
        if (!TextUtils.isEmpty(itemViewTitle)) {
            paint.setColor(titleTextColor);
            //paint.getTextBounds(itemViewTitle, 0, itemViewTitle.length(), mBounds);
            c.drawText(itemViewTitle, x, y, paint);
        }
    }

}
